package com.sandking.db.dao;

import com.sandking.db.bean.Yhljxx;
import javax.sql.DataSource;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.QueryRunner;
import com.sandking.config.SK_Config;
import java.sql.Connection;
import com.sandking.metadata.jdbc.SK_Query;
import java.util.Map;
import java.util.List;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.handlers.MapListHandler;

public class YhljxxDao {
	public static Yhljxx insert(Yhljxx yhljxx){
		Connection conn = SK_Config.getConnection();
		return insert(yhljxx,conn);
	}
	
	public static Yhljxx insert(Yhljxx yhljxx,Connection conn){
		return insert(yhljxx,conn,Yhljxx.TABLENAME);
	}
	
	public static Yhljxx insert(Yhljxx yhljxx,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insert(yhljxx,conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Yhljxx insert(Yhljxx yhljxx,String tableName){
		Connection conn = SK_Config.getConnection();
		return insert(yhljxx,conn,tableName);
	}
	
	public static Yhljxx insert(Yhljxx yhljxx,Connection conn,String tableName){
		
		SK_Query sq = new SK_Query();
		String sql = "INSERT INTO " +tableName+ " (id,用户_id,充值金额,掠夺金币,掠夺水,清除障碍数,技能升级次数,战胜玩家次数,充值元宝数,登录天数) VALUES (?,?,?,?,?,?,?,?,?,?)";
		try {
			int i = (int)sq.insert(conn,sql,yhljxx.getId(),yhljxx.getYh_id(),yhljxx.getCzje(),yhljxx.getLdjb(),yhljxx.getLds(),yhljxx.getQczas(),yhljxx.getJnsjcs(),yhljxx.getZswjcs(),yhljxx.getCzybs(),yhljxx.getDlts());
			if(yhljxx.getId()==0){
				yhljxx.setId(i);
			}
			return i > 0 ? yhljxx : null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
	}
	
	public static Yhljxx insert(Yhljxx yhljxx,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insert(yhljxx,conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	public static int[] insertBatch(List<Yhljxx> yhljxxs){
		Connection conn = SK_Config.getConnection();
		return insertBatch(yhljxxs,conn);
	}
	
	public static int[] insertBatch(List<Yhljxx> yhljxxs,Connection conn){
		return insertBatch(yhljxxs,conn,Yhljxx.TABLENAME);
	}
	
	public static int[] insertBatch(List<Yhljxx> yhljxxs,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(yhljxxs,conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static int[] insertBatch(List<Yhljxx> yhljxxs,String tableName){
		Connection conn = SK_Config.getConnection();
		return insertBatch(yhljxxs,conn,tableName);
	}
	
	public static int[] insertBatch(List<Yhljxx> yhljxxs,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (id,用户_id,充值金额,掠夺金币,掠夺水,清除障碍数,技能升级次数,战胜玩家次数,充值元宝数,登录天数) VALUES (?,?,?,?,?,?,?,?,?,?)";
		try {
			int columnSize = 10;
			int size = yhljxxs.size();
			Object[][] params = new Object[size][columnSize];
			for (int i = 0; i < size; i++) {
				params[i][0] =yhljxxs.get(i).getId();
				params[i][1] =yhljxxs.get(i).getYh_id();
				params[i][2] =yhljxxs.get(i).getCzje();
				params[i][3] =yhljxxs.get(i).getLdjb();
				params[i][4] =yhljxxs.get(i).getLds();
				params[i][5] =yhljxxs.get(i).getQczas();
				params[i][6] =yhljxxs.get(i).getJnsjcs();
				params[i][7] =yhljxxs.get(i).getZswjcs();
				params[i][8] =yhljxxs.get(i).getCzybs();
				params[i][9] =yhljxxs.get(i).getDlts();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 1 ? is : new int[]{};
		} catch (Exception e) {
			e.printStackTrace();
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
	}
	
	public static int[] insertBatch(List<Yhljxx> yhljxxs,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(yhljxxs,conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	public static Yhljxx update(Yhljxx yhljxx){
		Connection conn = SK_Config.getConnection();
		return update(yhljxx,conn);
	}
	
	public static Yhljxx update(Yhljxx yhljxx,Connection conn){
		return update(yhljxx,conn,Yhljxx.TABLENAME);
	}
	
	public static Yhljxx update(Yhljxx yhljxx,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return update(yhljxx,conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Yhljxx update(Yhljxx yhljxx,String tableName){
		Connection conn = SK_Config.getConnection();
		return update(yhljxx,conn,tableName);
	}
	
	public static Yhljxx update(Yhljxx yhljxx,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		StringBuffer sb = new StringBuffer();
		Map<String, Object> updateColumns = yhljxx.getUpdateColumns();
		int columnSize = updateColumns.size();
		if (updateColumns.isEmpty()) {
			return yhljxx;
		}
		sb.append("UPDATE ");
		sb.append(tableName);
		sb.append(" SET ");
		Object[] values = new Object[(columnSize + 1)];
		int i = 0;
		for (Map.Entry<String, Object> updateColumn : updateColumns.entrySet()) {
			String key = updateColumn.getKey();
			values[i] = updateColumn.getValue();
			i++;
			sb.append(key);
			sb.append("=");
			sb.append("?");
			if (i < columnSize) {
				sb.append(",");
			}
		}
		sb.append(" WHERE ");
		sb.append("id");
		sb.append(" = ?");
		values[columnSize] = yhljxx.getId();
		String sql = sb.toString();
		try {
			i = run.update(conn, sql, values);			
			return i == 1 ? yhljxx : null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally {
			try{
				yhljxx.clearUpdateColumn();
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
	}
	
	public static Yhljxx update(Yhljxx yhljxx,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return update(yhljxx,conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static int[] updateBatch(List<Yhljxx> yhljxxs){
		Connection conn = SK_Config.getConnection();
		return updateBatch(yhljxxs,conn);
	}
	
	public static int[] updateBatch(List<Yhljxx> yhljxxs,Connection conn){
		return updateBatch(yhljxxs,conn,Yhljxx.TABLENAME);
	}
	
	public static int[] updateBatch(List<Yhljxx> yhljxxs,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(yhljxxs,conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static int[] updateBatch(List<Yhljxx> yhljxxs,String tableName){
		Connection conn = SK_Config.getConnection();
		return updateBatch(yhljxxs,conn,tableName);
	}
	
	public static int[] updateBatch(List<Yhljxx> yhljxxs,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "UPDATE " +tableName+ " SET id = ?,用户_id = ?,充值金额 = ?,掠夺金币 = ?,掠夺水 = ?,清除障碍数 = ?,技能升级次数 = ?,战胜玩家次数 = ?,充值元宝数 = ?,登录天数 = ? WHERE id = ?";
		try {
			int columnSize = 10;
			int size = yhljxxs.size();
			Object[][] params = new Object[size][columnSize + 1];
			for (int i = 0; i < size; i++) {
				params[i][0] =yhljxxs.get(i).getId();
				params[i][1] =yhljxxs.get(i).getYh_id();
				params[i][2] =yhljxxs.get(i).getCzje();
				params[i][3] =yhljxxs.get(i).getLdjb();
				params[i][4] =yhljxxs.get(i).getLds();
				params[i][5] =yhljxxs.get(i).getQczas();
				params[i][6] =yhljxxs.get(i).getJnsjcs();
				params[i][7] =yhljxxs.get(i).getZswjcs();
				params[i][8] =yhljxxs.get(i).getCzybs();
				params[i][9] =yhljxxs.get(i).getDlts();
				params[i][columnSize] =yhljxxs.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 1 ? is : new int[]{};
		} catch (Exception e) {
			e.printStackTrace();
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
	}
	
	public static int[] updateBatch(List<Yhljxx> yhljxxs,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(yhljxxs,conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	public static boolean delete(Yhljxx yhljxx){
		Connection conn = SK_Config.getConnection();
		return delete(yhljxx,conn);
	}
	
	public static boolean delete(Yhljxx yhljxx,Connection conn){
		return delete(yhljxx,conn,Yhljxx.TABLENAME);
	}
	
	public static boolean delete(Yhljxx yhljxx,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return delete(yhljxx,conn);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean delete(Yhljxx yhljxx,String tableName){
		Connection conn = SK_Config.getConnection();
		return delete(yhljxx,conn,tableName);
	}
	
	public static boolean delete(Yhljxx yhljxx,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int i = run.update(conn,sql, yhljxx.getId());
			return i > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return false;
			}
		}
	}
	
	public static boolean delete(Yhljxx yhljxx,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return delete(yhljxx,conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	
	public static boolean deleteBatch(List<Yhljxx> yhljxxs){
		Connection conn = SK_Config.getConnection();
		return deleteBatch(yhljxxs,conn);
	}
	
	public static boolean deleteBatch(List<Yhljxx> yhljxxs,Connection conn){
		return deleteBatch(yhljxxs,conn,Yhljxx.TABLENAME);
	}
	
	public static boolean deleteBatch(List<Yhljxx> yhljxxs,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(yhljxxs,conn);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean deleteBatch(List<Yhljxx> yhljxxs,String tableName){
		Connection conn = SK_Config.getConnection();
		return deleteBatch(yhljxxs,conn,tableName);
	}
	
	public static boolean deleteBatch(List<Yhljxx> yhljxxs,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int size = yhljxxs.size();
			Object[][] params = new Object[size][1];
			for (int i = 0; i < size; i++) {
				params[i][0] = yhljxxs.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return false;
			}
		}
	}
	
	public static boolean deleteBatch(List<Yhljxx> yhljxxs,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(yhljxxs,conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 根据( id ) 查询
	 */
	public static Yhljxx getById(int id){
		Connection conn = SK_Config.getConnection();
		return getById(id, conn);
	}
	
	public static Yhljxx getById(int id,String tableName){
		Connection conn = SK_Config.getConnection();
		return getById(id, conn,tableName);
	}
	
	/**
	 * 根据( 用户_id ) 查询
	 */
	public static Yhljxx getByYh_id(int yh_id){
		Connection conn = SK_Config.getConnection();
		return getByYh_id(yh_id, conn);
	}
	
	public static Yhljxx getByYh_id(int yh_id,String tableName){
		Connection conn = SK_Config.getConnection();
		return getByYh_id(yh_id, conn,tableName);
	}
	
	
	//Connection
	/**
	 * 根据( id ) 查询
	 */
	public static Yhljxx getById(int id,Connection conn){
		return getById(id,conn,Yhljxx.TABLENAME);
	}
	
	public static Yhljxx getById(int id,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,用户_id,充值金额,掠夺金币,掠夺水,清除障碍数,技能升级次数,战胜玩家次数,充值元宝数,登录天数 FROM " + tableName + " WHERE " + "id = ?";
		Yhljxx yhljxx = null; 
		try {
			Map<String, Object> map = run.query(conn,sql, new MapHandler(), id);
			yhljxx = Yhljxx.createForColumnNameMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
		return yhljxx;
	}
	
	/**
	 * 根据( 用户_id ) 查询
	 */
	public static Yhljxx getByYh_id(int yh_id,Connection conn){
		return getByYh_id(yh_id,conn,Yhljxx.TABLENAME);
	}
	
	public static Yhljxx getByYh_id(int yh_id,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,用户_id,充值金额,掠夺金币,掠夺水,清除障碍数,技能升级次数,战胜玩家次数,充值元宝数,登录天数 FROM " + tableName + " WHERE " + "yh_id = ?";
		Yhljxx yhljxx = null; 
		try {
			Map<String, Object> map = run.query(conn,sql, new MapHandler(), yh_id);
			yhljxx = Yhljxx.createForColumnNameMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
		return yhljxx;
	}
	
	
	//DataSource
	/**
	 * 根据( id ) 查询
	 */
	public static Yhljxx getById(int id,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Yhljxx getById(int id,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据( 用户_id ) 查询
	 */
	public static Yhljxx getByYh_id(int yh_id,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getByYh_id(yh_id, conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Yhljxx getByYh_id(int yh_id,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getByYh_id(yh_id, conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	
	public static List<Yhljxx> getAll(){
		Connection conn = SK_Config.getConnection();
		return getAll(conn);
	}
	
	public static List<Yhljxx> getAll(Connection conn){
		return getAll(conn,Yhljxx.TABLENAME);
	}
	
	public static List<Yhljxx> getAll(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static List<Yhljxx> getAll(String tableName){
		Connection conn = SK_Config.getConnection();
		return getAll(conn,tableName);
	}
	
	public static List<Yhljxx> getAll(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,用户_id,充值金额,掠夺金币,掠夺水,清除障碍数,技能升级次数,战胜玩家次数,充值元宝数,登录天数 FROM " + tableName;
		List<Yhljxx> yhljxxs = null; 
		try {
			List<Map<String,Object>> list = run.query(conn, sql, new MapListHandler());
			yhljxxs = Yhljxx.createForColumnNameList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
		return yhljxxs;
	}
	
	public static List<Yhljxx> getAll(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	public static List<Yhljxx> getAllPage(int page,int pageCount){
		Connection conn = SK_Config.getConnection();
		return getAllPage(conn,page,pageCount);
	}
	
	public static List<Yhljxx> getAllPage(Connection conn,int page,int pageCount){
		return getAllPage(conn,Yhljxx.TABLENAME,page,pageCount);
	}
	
	public static List<Yhljxx> getAllPage(DataSource ds,int page,int pageCount){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,page,pageCount);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static List<Yhljxx> getAllPage(String tableName,int page,int pageCount){
		Connection conn = SK_Config.getConnection();
		return getAllPage(conn,tableName,page,pageCount);
	}
	
	public static List<Yhljxx> getAllPage(Connection conn,String tableName,int page,int pageCount){
		QueryRunner run = new QueryRunner();
		page = ((page-1) * pageCount);
		String sql = "SELECT id,用户_id,充值金额,掠夺金币,掠夺水,清除障碍数,技能升级次数,战胜玩家次数,充值元宝数,登录天数 FROM " + tableName + " LIMIT " + page + " , " +pageCount;
		List<Yhljxx> yhljxxs = null; 
		try {
			List<Map<String,Object>> list = run.query(conn, sql, new MapListHandler());
			yhljxxs = Yhljxx.createForColumnNameList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
		return yhljxxs;
	}
	
	public static List<Yhljxx> getAllPage(DataSource ds,String tableName,int page,int pageCount){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,tableName,page,pageCount);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
}